﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TWS.Cart.Data;

public partial class Application_Controls_Address : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

	private long _Id = -1;
	public long Id 
	{ 
		get { return _Id; }
		set { _Id = value; }
	}

	public string AddressLine1
	{
		get { return TextBoxStreetAndNumber1.Text; }
		set { TextBoxStreetAndNumber1.Text=value; }
	}

	public string AddressLine2
	{
		get { return TextBoxStreetAndNumber2.Text; }
		set { TextBoxStreetAndNumber2.Text = value; }
	}

	public string AddressLine3
	{
		get { return TextBoxStreetAndNumber3.Text; }
		set { TextBoxStreetAndNumber3.Text = value; }
	}

	public string City
	{
		get { return TextBoxCity.Text; }
		set { TextBoxCity.Text = value; }
	}

	public string State
	{
		get { return TextBoxState.Text; }
		set { TextBoxState.Text = value; }
	}

	public string ZipCode
	{
		get { return TextBoxZipCode.Text; }
		set { TextBoxZipCode.Text = value; }
	}

	/// <summary>
	/// Copies values from another Address Control.
	/// </summary>
	/// <param name="ctl">A Address Control</param>
	public void CopyValuesFromAddressControl(Application_Controls_Address ctl)
	{
		TextBoxStreetAndNumber1.Text = ctl.AddressLine1;
		TextBoxStreetAndNumber2.Text = ctl.AddressLine2;
		TextBoxStreetAndNumber3.Text = ctl.AddressLine3;
		TextBoxCity.Text = ctl.City;
		TextBoxState.Text = ctl.State;
		TextBoxZipCode.Text = ctl.ZipCode;
	}

	/// <summary>
	/// Clears all data in the control.
	/// </summary>
	public void Clear()
	{
		this.Id = -1;
		TextBoxStreetAndNumber1.Text="";
		TextBoxStreetAndNumber2.Text="";
		TextBoxStreetAndNumber3.Text="";
		TextBoxCity.Text="";
		TextBoxState.Text="";
		TextBoxZipCode.Text = "";
	}

	/// <summary>
	/// Saves the current address.
	/// </summary>
	/// <param name="addressTypeId">The type of addres 1=billing, 2=shipping, etc...</param>
	/// <returns>the saved ID or -1 if problem</returns>
	public long Save(int addressTypeId)
	{
		try
		{
			var ssAddress = new Address();
			ssAddress.AddressLine1 = TextBoxStreetAndNumber1.Text;
			ssAddress.AddressLine2 = TextBoxStreetAndNumber2.Text;
			ssAddress.AddressLine3 = TextBoxStreetAndNumber3.Text;
			ssAddress.AddressTypeId = addressTypeId;
			ssAddress.City = TextBoxCity.Text;
			ssAddress.State = TextBoxState.Text;
			ssAddress.ZipCode = TextBoxZipCode.Text;
			ssAddress.Save();
			Id = ssAddress.Id;
		}
		catch (Exception ex)
		{
			//Some error handling.
			Id = -1;
		}

		return Id;
	}

	/// <summary>
	/// Saves the address to the specified client.
	/// </summary>
	/// <param name="ClientId">The Id that represents the Client</param>
	/// <returns>True if success | False if fails.</returns>
	public bool SaveRelationToClient(long ClientId)
	{
		bool res = false;

		if(Id==-1)
			throw new ArgumentNullException("The Address_Id has to be greater than 0!");

		try
		{
			var ssClientAddress = new UsersAddress();
			ssClientAddress.UsersId = ClientId;
			ssClientAddress.AddressesId = this.Id;
			ssClientAddress.Save();
			res = true;
		}
		catch (Exception ex)
		{
			//Some error handling
		}

		return res;
	}


}
